//
//  MyAlertViewV3.m
//  BaseLib
//
//  Created by CPX on 2018/9/14.
//

#import "MyAlertViewV3.h"
#import "YYCategories.h"

/**
 基类弹框View
 @author yzl
 */
@interface MyAlertViewV3()

@property (nonatomic,strong) NSMutableArray *btns;
@property (nonatomic,strong) UIView *whiteView;
@property (nonatomic, strong) UIImageView *leftIV;
@property (nonatomic, strong) UIImageView *rightIV;
@property (nonatomic, strong) UIImageView *middelIV;

@end

@implementation MyAlertViewV3

- (void)initUI{
    [super initUI];
    
    self.whiteView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth - 80, 191)];
    self.whiteView.backgroundColor = [UIColor whiteColor];
    self.whiteView.layer.masksToBounds = YES;
    self.whiteView.layer.cornerRadius = 5;
    self.whiteView.center = self.contentView.center;
    [self.contentView addSubview:self.whiteView];
    
    self.titleLable = [UILabel new];
    self.titleLable.font = [UIFont systemFontOfSize:16];
    self.titleLable.width = self.whiteView.width - 48;
    self.titleLable.numberOfLines = 0;
    self.titleLable.textColor = UIColorHex(#000000);
    [self.whiteView addSubview:self.titleLable];
    
    self.leftIV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pop_left_img"]];
    [self.whiteView addSubview:self.leftIV];
    
    self.middelIV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pop_middle_img"]];
    [self.whiteView addSubview:self.middelIV];
    
    self.rightIV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pop_right_img"]];
    [self.whiteView addSubview:self.rightIV];
}

+ (instancetype)alertViewWithTitle:(NSString *)title block:(MyAlertBlockV3)block{
    MyAlertViewV3 *alert = [MyAlertViewV3 new];
    alert.removeOnTouchOutside = NO;
    alert.titleLable.text = title;
    alert.block = block;
    return alert;
}

- (void)addActionWithTitle:(NSString *)title color:(UIColor *)color{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitle:title forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:17];
    [btn addTarget:self action:@selector(onButtonTouched:) forControlEvents:UIControlEventTouchUpInside];
    [btn setTitleColor:color forState:UIControlStateNormal];
    btn.layer.borderColor = UIColorHex(#EFEFEF).CGColor;
    btn.layer.borderWidth = .5f;
    [self.whiteView addSubview:btn];
    [self.btns addObject:btn];
}

- (void)layoutSubviews{
    [super layoutSubviews];
    
    CGSize size = [self.titleLable sizeThatFits:CGSizeMake(self.titleLable.width, MAXFLOAT)];
    self.titleLable.height = size.height;
    self.titleLable.top = 38.5;
    
    self.titleLable.left = 24;
    
    self.whiteView.height = self.titleLable.bottom + 30 + 36+8;
    self.whiteView.center = self.contentView.center;
    
    self.leftIV.left = 0;
    self.rightIV.right = self.whiteView.width;
    self.middelIV.width = self.whiteView.width - self.leftIV.width-self.rightIV.width;
    self.middelIV.left = self.leftIV.right;
    
    CGFloat h = 52;
    CGFloat w = self.whiteView.width/(self.btns.count?self.btns.count:1);
//    CGFloat right = self.titleLable.superview.width - 8;
    CGFloat bottom = self.whiteView.height;
    CGFloat t1 = 0;
    CGFloat t2 = self.whiteView.width;
    for (NSInteger i = self.btns.count -1; i>=0; i--) {
        UIButton *btn = self.btns[i];
        btn.size = CGSizeMake(w, h);
        
        btn.bottom = bottom;
        if ([UIView appearance].semanticContentAttribute == UIWebPaginationModeRightToLeft) {
            btn.left = t1;
            t1+=w;
        }
        else{
            
            
            btn.right = t2;
            t2-= w;
        }
    }
}

- (NSMutableArray*)btns{
    if (!_btns) {
        _btns = [NSMutableArray array];
    }
    return _btns;
}

- (void)onButtonTouched:(UIButton*)sender{
    NSInteger idx = [self.btns indexOfObject:sender];
    if (self.block) {
        self.block(idx);
    }
    [self removeFromSuperview];
}

@end
